import utils from '@common/utils';

const fullMenus = [
  {
    title: '首页',
    key: 'Home',
    icon: 'icon-monitor',
    show: true
  },
  {
    title: '平台设置',
    key: 'SysSetting',
    icon: 'icon-cog',
    show: true,
    children: [
      {
        title: '个人中心',
        key: 'AccountBasic',
        show: true
      },
      {
        title: '安全设置',
        key: 'SecuritySetting',
        show: true
      }
      // {
      //   title: '权限设置',
      //   key: 'Authorization',
      //   show: true
      // }
    ]
  },
  {
    title: '宝贝管理',
    key: 'Product',
    icon: 'icon-heart',
    show: true
  },
  {
    title: '订单列表',
    key: 'Order',
    icon: 'h-icon-menu',
    show: true,
    children: [
      {
        title: '订单详情',
        key: 'OrderDetail',
        show: false
      }
    ]
  },
  {
    title: '列表应用',
    key: 'TableList',
    icon: 'icon-grid-2',
    show: true,
    children: [
      {
        title: '基础表格',
        key: 'TableBasic',
        show: true
      },
      {
        title: '查询列表',
        key: 'TableSearch',
        show: true
      }
    ]
  },
  {
    title: '表单应用',
    key: 'FormFolder',
    icon: 'icon-paper',
    show: true,
    children: [
      {
        title: '基础表单',
        key: 'Form',
        show: true
      },
      {
        title: '表单详情',
        key: 'FormDetail',
        show: true
      }
    ]
  },
  {
    title: '模糊匹配',
    key: 'AutoCompleteFolder',
    icon: 'icon-disc',
    show: true,
    children: [
      {
        title: '模糊搜索',
        key: 'Autocomplete1',
        show: true
      },
      {
        title: '场景应用',
        key: 'Autocomplete2',
        show: true
      },
      {
        title: '复杂场景',
        key: 'Autocomplete3',
        show: true
      }
    ]
  },
  {
    title: '扩展组件',
    key: 'Advance-folder',
    icon: 'icon-bar-graph-2',
    show: true,
    children: [
      {
        title: '图表',
        key: 'Chart',
        show: true
      },
      {
        title: '富文本编辑器',
        key: 'RicktextEditor',
        show: true
      },
      {
        title: '代码编辑器',
        key: 'CodeEditor',
        show: true
      },
      {
        title: 'Markdown编辑器',
        key: 'MarkdownEditor',
        show: true
      },
      {
        title: '百度地图',
        key: 'BaiduMap',
        show: true
      }
    ]
  },
  {
    title: '教学文档',
    key: 'Doc',
    icon: 'h-icon-link',
    show: false
  }
];

const getMenus = function (menuIdList = []) {
  return getAuthMenu(fullMenus, menuIdList);
};

let getAuthMenu = (menus, menuIdList) => {
  let configMenu = [];
  for (let menu of menus) {
    let m = utils.copy(menu);
    if (menuIdList.indexOf(m.key) > -1) {
      if (m.show) {
        configMenu.push(m);
      }
    }
    if (menu.children && menu.children.length) {
      m.children = getAuthMenu(menu.children, menuIdList);
    }
  }
  return configMenu;
};

const getKeys = function (menus) {
  let keys = [];
  for (let menu of menus) {
    keys.push(menu.key);
    if (menu.children && menu.children.length) {
      keys.push(...getKeys(menu.children));
    }
  }
  return keys;
};

let fullMenuKeys = getKeys(fullMenus);

const isAuthPage = function (menus, name) {
  if (fullMenuKeys.indexOf(name) > -1 && menus.indexOf(name) == -1) {
    return false;
  }
  return true;
};

export { getMenus, fullMenus, fullMenuKeys, isAuthPage };
